/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();

var bingliController = {
    /**
     *  危重症时间 病例讨论
     * @param req
     * @param res
     */
    getJWBingliList: function (req, res) {
        co(function () {
            var item = parseInt(req.query.item);
            var sort = parseInt(req.query.sort);
            // var title = "";
            // if(req.query.title!=undefined && req.query.title!=null){
            //     title = req.query.title;
            // }
            // var blOldSql = " select b.id,b.title,b.title_pic as titlePic,b.attach_type as attachType,b.attach_video as attachVideo,b.attach_ppt as attachPPT," +
            //     " b.description,b.score_c as scoreC,b.top_time as topTime,b.create_time as createTime," +
            //     " count(ck.id) as cCount,count(cm.id) as pCount,count(se.id) as zCount " +
            //     " from lm_bl as b" +
            //     " left join lm_click_log as ck on  ck.title_id = b.ID" +
            //     " left join lm_comment_log as cm on cm.title_id = b.ID" +
            //     " left join lm_share_log as se on se.title_id = b.ID" +
            //     " where  b.sort="+sort+"  and b.item="+item+" and b.title like '%"+title+"%'" +
            //     " GROUP by b.id order by b.top_time,b.create_time";
            var blNewSql = "select b.id,b.title,b.title_pic as titlePic,b.attach_type as attachType,b.attach_video as attachVideo,b.attach_ppt as attachPPT," +
                " b.description,b.score_c as scoreC,b.top_time as topTime,b.create_time as createTime,(select count(*) from lm_click_log where title_id = b.id) as cCount," +
                "(select count(*) from lm_comment_log where title_id = b.id) as pCount,(select count(*) from lm_share_log where title_id = b.id) as zCount from lm_bl as b where 1=1 ";
            blNewSql += " and b.item = " + item + " and b.sort = " + sort;

            if (req.query.title != undefined && req.query.title != null) {
                blNewSql += " and (b.title like '%" + req.query.title + "%' or b.expert_name like '%" + req.query.title + "%')";
            }
            blNewSql += " order by b.top_time DESC";
            seq.sequelize.query(blNewSql).spread(function (result) {
                res.json({ "success": true, "msg": "查询成功！", "result": result });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    /**
     *   急危e频道接口 病例e频道
     * @param req
     * @param res
     */
    getBingli: function (req, res) {
        co(function () {

            var params = {};
            var paramT = {};
            //根据id
            if (req.query.id != undefined) {
                params["id"] = req.query.id;
            }
            if (req.query.sort != undefined) {
                params["sort"] = req.query.sort;
            }
            if (req.query.type != undefined) {
                params["type"] = req.query.type;
            }
            if (req.query.item != undefined) {
                params["item"] = req.query.item;
            }

            paramT["where"] = params;
            paramT["order"] = [["topTime", 'DESC']];

            seq.bingliModule.findOne(paramT).then(function (result) {
                res.json({ "success": true, "result": result });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    /**
     *  脑血管时间 病例讨论
     * @param req
     * @param res
     */
    getZZBingliList: function (req, res) {
        co(function () {
            var item = parseInt(req.query.item);
            var sort = parseInt(req.query.sort);
            // var title = "";
            // if(req.query.title!=undefined&&req.query.title!=null){
            //     title = req.query.title;
            // }
            // var oldSql = " select b.id,b.title,b.title_pic as titlePic,b.attach_type as attachType,b.attach_video as attachVideo,b.attach_ppt as attachPPT," +
            //     " b.description,b.score_c as scoreC,b.top_time as topTime,b.create_time as createTime," +
            //     " count(ck.id) as cCount,count(cm.id) as pCount,count(se.id) as zCount " +
            //     " from lm_bl as b" +
            //     " left join lm_click_log as ck on  ck.title_id = b.ID" +
            //     " left join lm_comment_log as cm on cm.video_id = b.ID" +
            //     " left join lm_share_log as se on se.title_id = b.ID" +
            //     " where  b.sort="+sort+"  and b.item="+item+" and b.title like '%"+title+"%'" +
            //     " GROUP by b.id order by b.top_time,b.create_time";
            var blNewSql = "select b.id,b.title,b.title_pic as titlePic,b.attach_type as attachType,b.attach_video as attachVideo,b.attach_ppt as attachPPT," +
                " b.description,b.score_c as scoreC,b.top_time as topTime,b.create_time as createTime,(select count(*) from lm_click_log where title_id = b.id) as cCount," +
                "(select count(*) from lm_comment_log where title_id = b.id) as pCount,(select count(*) from lm_share_log where title_id = b.id) as zCount from lm_bl as b where 1=1 ";
            blNewSql += " and b.item = " + item + " and b.sort = " + sort;

            if (req.query.title != undefined && req.query.title != null) {
                blNewSql += " and (b.title like '%" + req.query.title + "%' or b.expert_name like '%" + req.query.title + "%')";
            }
            blNewSql += " order by b.top_time DESC";
            seq.sequelize.query(blNewSql).spread(function (result) {
                res.json({ "success": true, "result": result });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    /**
     *  根据ID查询病例详情
     * @param req
     * @param res
     */
    getJWbingliDetil: function (req, res) {
        co(function () {
            var params = {};
            var paramT = {};
            params["id"] = req.params.ID;
            paramT["where"] = params;
            seq.bingliModule.findAndCountAll(paramT).then(function (result) {
                res.json({ "success": true, "total": result.count, "result": result.rows });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    }




}
module.exports = bingliController;